/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import jpa.controller.EtapaJpaController;
import jpa.entity.Etapa;

/**
 *
 * @author isaias
 */
@ManagedBean
@ViewScoped
public class CentroCustoBean implements Serializable{
    private String centroCustoFilter = "";
    private Etapa etapa = new Etapa();
    private Etapa etapaSelected = new Etapa();

    /** Creates a new instance of CentroCustoBean */
    public CentroCustoBean() {
    }

    public void save(){
        try{
            new EtapaJpaController().create(etapa);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Centro de custo criado com sucesso!", null));
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao tentar criar o centro de custo!", null));
        }
    }

    public void remove(){
        try {
            
                System.out.println(">>>>>>> etapa: "+etapaSelected.getName());
                System.out.println(">>>>>>> etapa: "+etapaSelected.getId());
                new EtapaJpaController().destroy(etapaSelected.getId());
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Centro de custo removido com sucesso!", null));
           
        } catch (Exception e) {
              e.printStackTrace();
               FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao tentar remover o centro de custo selecionado!", null));
        }
    }

    public Etapa getEtapa() {
        return etapa;
    }

    public List<Etapa> getEtapaList(){
        return new EtapaJpaController().findEtapaEntitiesByName(centroCustoFilter);
    }

    public void setEtapa(Etapa etapa) {
        this.etapa = etapa;
    }

    public String getCentroCustoFilter() {
        return centroCustoFilter;
    }

    public void setCentroCustoFilter(String centroCustoFilter) {
        this.centroCustoFilter = centroCustoFilter;
    }

    public Etapa getEtapaSelected() {
        return etapaSelected;
    }

    public void setEtapaSelected(Etapa etapaSelected) {
        this.etapaSelected = etapaSelected;
    }
    
}
